#!/usr/bin/env python
# -*- coding: utf-8 -*-

from Shopping.core import auth
from Shopping.core.auth import login_required
from Shopping.core.logger import logger
from Atm.core.main import pay  # ATM支付接口

product_list = [
    ("iPhone", 5800),
    ("Mac Pro", 10600),
    ("iWatch", 2600),
    ("Book", 88)
]

user_data = {
    'account_id': None,
    'is_authenticated': False
}

shopping_list = []

@login_required
def shopping(user_data):
    while True:
        for index, item in enumerate(product_list):
            print(index, item)
        user_choice = input("[q退出]请选择要购买的商品:").strip()
        if user_choice .isdigit():
            user_choice = int(user_choice)
            if user_choice < len(product_list) and user_choice >= 0:
                product_item = product_list[user_choice]
                shopping_list.append(product_item)
                print("添加 {0} 商品到购物车...".format(product_item[0]))
            else:
                print("此商品 {0} 不存在...".format(user_choice))
        elif user_choice == 'q':
            break
        else:
            print("您输入的选项错误,请重新输入...")

@login_required
def show_shopping_cart(user_data):
    menu = '''-------- 购物车操作 --------
        1. 删除商品
        2. 付款
        3. 退出
    '''
    while True:
        if shopping_list:
            print('shopping cart'.center(30, '-'))  # 判断购物车是否为空
            for index, item in enumerate(shopping_list):
                print(index, item)
        else:
            print('购物车中没有商品...')
            break
        print(menu)
        choose = input('请选择操作:').strip()
        if choose.isdigit():
            if choose == '3':break
            if choose == '1':
                choose_prod = input('请选择要删除的商品序号:').strip()
                if choose_prod.isdigit():
                    choose_prod = int(choose_prod)
                    if choose_prod >= 0 and choose_prod < len(shopping_list):
                        print(shopping_list)
                        if shopping_list:
                            print('%s 商品已从购物车中删除...' % shopping_list[choose_prod][0])
                            shopping_list.pop(choose_prod)
                    else:
                        print('没有此商品...')
                else:
                    print('你输入的选项错误,请输入整数...')
            elif choose == '2':
                amount = 0
                for item in shopping_list:
                    amount += item[1]
                pay_amount = pay(amount)
                if pay_amount:
                    shopping_list_temp = []
                    for item in shopping_list:
                        shopping_list_temp.append(item[0])
                    logger(user_data['account_id'], '购买了 '+str(shopping_list_temp)+' 共支付金额: ￥%s' % amount)
                    print('支付成功,共支付: ￥%s' % amount)
                    shopping_list.clear()   # 清空购物车
                    break
            else:
                print('你输入的选项不存在...')

def interactive(acc_data):
    '''
    用户交互
    :param acc_data: 用户的所有数据
    :return:
    '''
    menu = '''-------- shopping --------
    1. 购物
    2. 查看购物车
    3. 退出
    '''
    menu_dic = {
        '1': shopping,
        '2': show_shopping_cart,
    }
    flag = True
    while flag:
        print(menu)
        choose = input('请选择:').strip()
        if choose in menu_dic:
            menu_dic[choose](acc_data)
        elif choose == '3':
            print('退出购物商城!!!')
            flag = False
        else:
            print('您输入的选项不存在...')

def run():
    '''
    程序启动时调用此函数,处理用户交互数据
    :return:
    '''
    acc_data = auth.acc_login(user_data)
    if user_data['is_authenticated']:
        user_data['account_data'] = acc_data
        interactive(user_data)
